package com.yurun.loginmodule.controller;

import com.github.pagehelper.PageHelper;
import com.yurun.commonapi.api.CommonResult;
import com.yurun.commonapi.pojo.Branch;
import com.yurun.commonapi.pojo.Employee;
import com.yurun.employeemodule.service.EmployeeService;
import com.yurun.loginmodule.util.JWTUtil;
import com.yurun.orgbranch.service.BranchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletRequest;
import java.math.BigInteger;
import java.util.List;

/**
 * @author 吼吼权
 * @mail houhouquan@163.com
 * @Date 2021/8/18 10:46 下午
 */
@RestController
@Api("分店模块")
@RequestMapping("/branchManage")
public class BranchController {

    @Autowired
    private BranchService branchService;
    @Autowired
    private EmployeeService employeeService;


    @ApiOperation("获取所有机构的所有分店信息")
    @RequiresPermissions("/getAllBranch")
    @RequestMapping("/getAllBranch")
    public CommonResult getAllBranch(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<Branch> list = branchService.getAllBranch();
        return CommonResult.success(list);
    }

    @ApiOperation("获取所有机构的所有分店名")
    @RequestMapping("/getBranchOption")
    public CommonResult getBranchOption(ServletRequest request){
        // 获取token
        String token = WebUtils.toHttp(request).getHeader("Authorization").substring(6);
        // 获取机构id
        String organizationId = JWTUtil.getOrganizationId(token);

        return CommonResult.success(branchService.getBranchOption(BigInteger.valueOf(Long.parseLong(organizationId))));
    }

    @ApiOperation(value = "获取某机构的所有分店信息")
    @RequiresPermissions("/getBranchList")
    @RequestMapping("/getBranchList")
    public CommonResult getBranchInfo(ServletRequest request, String keyword, int pageNum, int pageSize) {
        String token = WebUtils.toHttp(request).getHeader("Authorization");
        String employeeId = JWTUtil.getUsername(token.substring(6));

        PageHelper.startPage(pageNum, pageSize);
        List<Branch> list = branchService.getBranchList(keyword, BigInteger.valueOf(Long.parseLong(employeeId)));
        if (list.size() == 0) {
            return CommonResult.failed("对不起，暂时查询不到分店信息！");
        }
        return CommonResult.success(list);
    }

    @ApiOperation(value = "通过id获取某分店详细信息")
    @RequiresPermissions("/BranchInfoById")
    @RequestMapping("/BranchInfoById")
    public CommonResult getBranchInfo(BigInteger organizationId, BigInteger branchId) {
        Branch branch = branchService.getBranchInfoById(organizationId,branchId);
        return CommonResult.success(branch);
    }

    @ApiOperation(value = "通过分店名获取某分店详细信息")
    @RequiresPermissions("/getBranchByName")
    @RequestMapping("/getBranchByName")
    public CommonResult getBranchByName(ServletRequest request, String branchName, int pageNum, int pageSize){
        String token = WebUtils.toHttp(request).getHeader("Authorization");
        String employeeId = JWTUtil.getUsername(token.substring(6));
        PageHelper.startPage(pageNum, pageSize);
        List<Branch> list  = branchService.getBranchByName(branchName, BigInteger.valueOf(Long.parseLong(employeeId)));
        if (list.size() == 0) {
            return CommonResult.failed("对不起，暂时查询不到该分店！");
        }
        return CommonResult.success(list);
    }

    @ApiOperation(value = "逻辑删除机构")
    @RequiresPermissions("/delBranch")
    @RequestMapping("/delBranch")
    public CommonResult delBranch(Branch branch) {
        return CommonResult.success(branchService.delBranchById(branch));
    }

    @ApiOperation(value = "新增机构")
    @RequiresPermissions("/addBranch")
    @RequestMapping("/addBranch")
    public CommonResult addBranch(ServletRequest request, Branch branch) {
        String token = WebUtils.toHttp(request).getHeader("Authorization");
        String employeeId = JWTUtil.getUsername(token.substring(6));
        Employee employee = employeeService.getEmployeeById(BigInteger.valueOf(Long.parseLong(employeeId)));
        BigInteger organizationId = employee.getOrganizationId();
        branch.setOrganizationId(organizationId);
        boolean result = branchService.addBranch(branch);
        if (result) {
            return CommonResult.success("分店增加成功！");
        }
        return CommonResult.failed("增加分店失败！");
    }

    @ApiOperation(value = "修改机构")
    @RequiresPermissions("/editBranch")
    @RequestMapping("/editBranch")
    public CommonResult editBranch(Branch branch) {
        return CommonResult.success(branchService.editBranch(branch));
    }
}